//
//  ZGLLoginStatusView.m
//  ZeegingLottery
//
//  Created by 蒂埃里 on 2018/3/29.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//
//登录状态view

#import "ZGLLoginStatusView.h"

@interface ZGLLoginStatusView ()<UITableViewDelegate,UITableViewDataSource>
{
    FBKVOController *_fbKVO;
}


@property (strong, nonatomic)UIImageView *iconImage;
@property (strong, nonatomic)UILabel *nameLabel;
@property (strong, nonatomic)UITextField *accountTextField;    //
@property (strong, nonatomic)UITextField *passwordTextField;

@property (strong, nonatomic)UILabel *yuELabel;
@property (strong, nonatomic)UILabel *yuENumLabel;  //余额
@property (strong, nonatomic)UILabel *hongBaoLabel;
@property (strong, nonatomic)UILabel *hongBaoNumLabel; //红包

@property (strong, nonatomic)UITableView *functionListView;



@end

@implementation ZGLLoginStatusView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        self.backgroundColor = ViewBackgroundColor;
        
        _fbKVO = [FBKVOController controllerWithObserver:self];
        ZGLUserInfoModel *model = [ZGLUserInfoModel shardModel];
//        NSLog(@"%@",model);
        _isLogin = model.usercheck;
        
        BOOL isLogin = self.isLogin;
        if (isLogin) {

            [self setupLoginView];

        }else{

            [self setupUnLoginView];

        }
        
        //登录状态改变时触发KVO
        [_fbKVO observe:model keyPath:@"usercheck" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, ZGLUserInfoModel *model, NSDictionary<NSString *,id> * _Nonnull change) {
//            NSLog(@"---%@====%@",change,model);
//            NSString *changeKeyPath = change[FBKVONotificationKeyPathKey];
            if (model.usercheck == YES) {
                for (UIView *view in [self subviews]) {
                    [view removeFromSuperview];
                }
                [self setupLoginView];
            }else{
                for (UIView *view in [self subviews]) {
                    [view removeFromSuperview];
                }
                
                [self setupUnLoginView];
            }
        }];
    }
    
    return self;
}

//已登录
- (void)setupLoginView
{
    [self addSubview:self.iconImage];
    [self addSubview:self.nameLabel];
    [self addSubview:self.yuELabel];
    [self addSubview:self.yuENumLabel];
    [self addSubview:self.hongBaoLabel];
    [self addSubview:self.hongBaoNumLabel];
    
    [self addSubview:self.functionListView];
    
    CGFloat buttonH;
    CGFloat margin;
    CGFloat BtnMargin;
    
    if (IS_IPHONE_5andSE) {
        buttonH = 40;
        margin = 20;
        BtnMargin = 10;
    }else {
        buttonH = 45;
        margin = 35;
        BtnMargin = 15;
    }

    self.iconImage.layer.cornerRadius = 30;
    self.iconImage.layer.masksToBounds = YES;
    [_iconImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self).offset(25);
        make.height.mas_equalTo(60);
        make.width.mas_equalTo(60);
    }];
    
    _nameLabel.text = @"您好";
    _nameLabel.textAlignment = 0;
    [_nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImage.mas_right).offset(10);
        make.right.equalTo(self);
        make.centerY.equalTo(_iconImage);
        
    }];
    
    
    [_yuELabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(-60);
        make.top.equalTo(_iconImage.mas_bottom).offset(margin);
        
    }];
    
    NSString *yuEStr = [PublicMethod DigitalNumberWithString:sZGLUserInfoModel.balance];
//    NSString *yuEStr = [PublicMethod digitUppercase:sZGLUserInfoModel.balance];
    _yuENumLabel.text = [NSString stringWithFormat:@"￥%@",yuEStr];
    [_yuENumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_yuELabel);
        make.top.equalTo(_yuELabel.mas_bottom).offset(5);
        
    }];
    
    
    [_hongBaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(60);
        make.bottom.equalTo(_yuELabel);
        
    }];
    
    NSString *hongbaoStr = [PublicMethod DigitalNumberWithString:sZGLUserInfoModel.red_cash];
//    NSString *hongbaoStr = [PublicMethod digitUppercase:sZGLUserInfoModel.red_cash];
    _hongBaoNumLabel.text = [NSString stringWithFormat:@"￥%@",hongbaoStr];
    [_hongBaoNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_hongBaoLabel);
        make.top.equalTo(_yuENumLabel);
        
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = BorderColor;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_yuELabel.mas_top);
        make.bottom.equalTo(_yuENumLabel.mas_bottom);
        make.centerX.equalTo(self);
        make.width.equalTo(@(kBorderWidth));
        
    }];
    
    
    [_functionListView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_hongBaoNumLabel.mas_bottom).offset(margin);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.equalTo(@(300));
        
    }];
    
}

//未登录
- (void)setupUnLoginView
{
    [self addSubview:self.accountTextField];
    [self addSubview:self.passwordTextField];
    [self addSubview:self.loginBtn];
    [self addSubview:self.registerBtn];
    [self addSubview:self.iconImage];
    [self addSubview:self.nameLabel];
    
    
    WS(weakself);
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(20);
        make.right.equalTo(weakSelf).offset(-20);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(weakSelf).offset(30);
    }];
    
    [_accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.passwordTextField);
        make.right.equalTo(weakSelf.passwordTextField);
        make.height.equalTo(weakSelf.passwordTextField);
        make.bottom.equalTo(weakSelf.passwordTextField.mas_top).offset(-5);
    }];
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = BorderColor;
    [self addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.passwordTextField);
        make.right.equalTo(weakSelf.passwordTextField);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(_accountTextField.mas_bottom).offset(1);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = BorderColor;
    [self addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(line1);
        make.right.equalTo(line1);
        make.height.equalTo(line1);
        make.top.equalTo(_passwordTextField.mas_bottom).offset(1);
        
    }];
    
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.passwordTextField);
        make.right.equalTo(weakSelf.passwordTextField);
        make.height.mas_equalTo(45);
        make.centerY.equalTo(weakSelf).offset(100);
        
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.loginBtn);
        make.right.equalTo(weakSelf.loginBtn);
        make.height.equalTo(weakSelf.loginBtn);
        make.top.equalTo(weakSelf.loginBtn.mas_bottom).offset(10);
        
    }];
    
    _nameLabel.textAlignment = 1;
    _nameLabel.text = @"您尚未登录，请先登录或注册";
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.passwordTextField);
        make.right.equalTo(weakSelf.passwordTextField);
        make.bottom.equalTo(weakSelf.accountTextField.mas_top).offset(-40);
        make.height.mas_equalTo(22);
        
    }];

    
    _iconImage.layer.cornerRadius = 35;
    _iconImage.layer.masksToBounds = YES;
    [_iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf);
        make.bottom.equalTo(weakSelf.nameLabel.mas_top).offset(-10);
        make.height.mas_equalTo(70);
        make.width.mas_equalTo(70);
        
    }];
    
}

#pragma mark - 懒加载

- (UITableView *)functionListView
{
    if (!_functionListView) {
        _functionListView = [[UITableView alloc]init];
        _functionListView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _functionListView.scrollEnabled = NO;
        _functionListView.backgroundColor = ViewBackgroundColor;
        _functionListView.delegate = self;
        _functionListView.dataSource = self;
        
    }
    return _functionListView;
}

- (UITextField *)accountTextField
{
    if (!_accountTextField) {
        _accountTextField = [[UITextField alloc]init];
        _accountTextField.placeholder = @"手机号码";
        _accountTextField.keyboardType = UIKeyboardTypeNumberPad;
        _accountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    
    return _accountTextField;
}

- (UITextField *)passwordTextField
{
    if (!_passwordTextField) {
        _passwordTextField = [[UITextField alloc]init];
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    
    return _passwordTextField;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.textColor = sRGBHexAlpha(0xF63F3F, 1.0);
        _loginBtn.backgroundColor = globalRed;
        [_loginBtn addTarget:self action:@selector(loginBtnAcction:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.layer.cornerRadius = 5;
        _loginBtn.layer.masksToBounds = YES;
    }
    
    return _loginBtn;
}

- (UIButton *)registerBtn
{
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:globalRed forState:UIControlStateNormal];
        _registerBtn.backgroundColor = [UIColor whiteColor];
        [_registerBtn addTarget:self action:@selector(registerBtnAcction:) forControlEvents:UIControlEventTouchUpInside];
        _registerBtn.layer.cornerRadius = 5;
        _registerBtn.layer.masksToBounds = YES;
        _registerBtn.layer.borderWidth = kBorderWidth;
        _registerBtn.layer.borderColor = BorderColor.CGColor;
        
    }
    
    return _registerBtn;
}

- (UIImageView *)iconImage
{
    if (!_iconImage) {
        _iconImage = [[UIImageView alloc]init];
        _iconImage.image = [UIImage imageNamed:@"icon512"];
        _iconImage.layer.borderColor = BorderColor.CGColor;
        _iconImage.layer.borderWidth = kBorderWidth;
//        _iconImage.layer.cornerRadius = 35;
//        _iconImage.layer.masksToBounds = YES;
    }
    
    return _iconImage;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textAlignment = 1;
        _nameLabel.font = [UIFont systemFontOfSize:14];
        _nameLabel.textColor = mainTextColor;
    }
    
    return _nameLabel;
}

- (UILabel *)yuELabel
{
    if (!_yuELabel) {
        _yuELabel = [[UILabel alloc]init];
        _yuELabel.text = @"账户余额";
        _yuELabel.font = [UIFont systemFontOfSize:12];
        _yuELabel.textAlignment = 1;
        _yuELabel.textColor = mainTextColor;
    }
    return _yuELabel;
}

- (UILabel *)yuENumLabel
{
    if (!_yuENumLabel) {
        _yuENumLabel = [[UILabel alloc]init];
        _yuENumLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        _yuENumLabel.textAlignment = 1;
        _yuENumLabel.textColor = mainTextColor;
        _yuENumLabel.text = @"0.00";
    }
    
    return _yuENumLabel;
}

- (UILabel *)hongBaoLabel
{
    if (!_hongBaoLabel) {
        _hongBaoLabel = [[UILabel alloc]init];
        _hongBaoLabel.font = [UIFont systemFontOfSize:13];
        _hongBaoLabel.textAlignment = 1;
        _hongBaoLabel.text = @"红包";
        _hongBaoLabel.textColor = mainTextColor;
    }
    return _hongBaoLabel;
}

- (UILabel *)hongBaoNumLabel
{
    if (!_hongBaoNumLabel) {
        _hongBaoNumLabel = [[UILabel alloc]init];
        //字体加粗
        _hongBaoNumLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        _hongBaoNumLabel.textAlignment = 1;
        _hongBaoNumLabel.textColor = mainTextColor;
        _hongBaoNumLabel.text = @"0.00";
    }
    return _hongBaoNumLabel;
}



#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *titleArr = @[@[@"充值",@"提现",@"购彩记录",@"关于我们"],@[@"退出登录"]];
    
    static NSString *const cellID = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.text = titleArr[indexPath.section][indexPath.row];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 40)];
    footView.backgroundColor = ViewBackgroundColor;
    
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"第%ld组第%ld个",indexPath.section,indexPath.row);
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                [self chargeButtonAction];
                break;
            case 1:
                [self tiXianButttonAction];
                break;
            case 2:
                [self gouCaiJiLuBtnAction];
                break;
            case 3:
                [self aboutOfUsAction];
                break;
                
        }
    }else{
        switch (indexPath.row) {
            case 0:
                [self loginOutButtonAction];
                break;
                
        }
    }
}


#pragma mark - meth

//充值
- (void)chargeButtonAction
{
    if (self.chargeButtonTouchBlock) {
        self.chargeButtonTouchBlock();
    }
    
}

//提现
- (void)tiXianButttonAction
{
    if (self.tiXianButttonTouchBlock) {
        self.tiXianButttonTouchBlock();
    }
    
}

//购彩记录
- (void)gouCaiJiLuBtnAction
{
    if (self.gouCaiJiLuBtnTouchBlock) {
        self.gouCaiJiLuBtnTouchBlock();
    }
    
}

//关于我们
- (void)aboutOfUsAction
{
    if (self.aboutUsCellTouhBlock) {
        self.aboutUsCellTouhBlock();
    }
    
}

//退出登录
- (void)loginOutButtonAction
{
    if (self.loginOutButtonTouchBlock) {
        self.loginOutButtonTouchBlock();
    }
    
}


//登录
- (void)loginBtnAcction:(UIButton *)sender
{
    NSString *acount = self.accountTextField.text;
    NSString *password = self.passwordTextField.text;
    if (self.loginButtonTouchBlock) {
        self.loginButtonTouchBlock(acount, password);
    }
}

//注册
- (void)registerBtnAcction:(UIButton *)sender
{
    if (self.registerButtonTouchBlock) {
        self.registerButtonTouchBlock(sender);
    }
}


@end
